<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/30/030
 * Time: 16:02
 */

class AppinfoController extends ControllerBase
{
    public function initialize()
    {
        parent::initialize();
        //$this->checkHasLogin();
    }

    public function listAction()
    {
        $wxh_name = $this->request->get("wxh_name");
        $type = $this->request->get("type");
        $where = "";
        if ($wxh_name) {
            $where .= "and info.nick_name like '%{$wxh_name}%'";
        }
        if ($type) {
            $where .= "and info.type like '%{$type}%'";
        }
        $list = $this->db->fetchAll("SELECT app.* ,
                                            info.nick_name,
                                            info.principal_name,
                                            info.head_img,
                                            info.type
                                            from authorized_app as app
                                           LEFT JOIN authorized_app_info as info on app.app_id = info.app_id
                                           WHERE app.enabled = 1 {$where}");
        //未绑定默认绑定第一个
        $username = $this->getMobie();
        if (empty($username)) {
            $this->exitMessage(false, '用户信息错误');
        }

        $bind_exit = BindInfo::findFirst("username='{$username}'");
        if (!$bind_exit) {
            $bind_info = New BindInfo();
            $bind_info->username = $username;
            $bind_info->bind_app_id = $list[0]['app_id'];
            $bind_info->save();
        }

        $this->exitMessage(true, '', $list);
    }

    /**
     * 更新公众号类别
     */
    public function updateTypeAction()
    {
        $list = AuthorizedAppInfo::find();
        foreach ($list as $val){
            $gzh_info = $this->zwbi->fetchOne("select `type`,gz_name from weixin where raw_id = '{$val->raw_id}' and status > -2");
            if($gzh_info){
                $val->nick_name = $gzh_info['gz_name'];
                $val->type = $gzh_info['type'];
                $val->save();
            }
        }

        $this->exitMessage(true, '公众号类别更新成功');
    }

    public function getBindInfoAction()
    {
        $username = $this->getMobie();
        if (empty($username)) {
            $this->exitMessage(false, '用户信息错误');
        }
        $bind_info = BindInfo::findFirst("username='{$username}'");
        $app_info = [];
        if($bind_info){
            $app_info = $this->db->fetchOne("SELECT app.* ,
                                            info.nick_name,
                                            info.principal_name,
                                            info.head_img
                                            from authorized_app as app
                                           LEFT JOIN authorized_app_info as info on app.app_id = info.app_id
                                           WHERE info.enabled = 1 and info.app_id='{$bind_info->bind_app_id}'");
        }
        $this->exitMessage(true, '', $app_info);
    }

    public function bindAction()
    {
        $app_id = $this->request->get('app_id');
        $username = $this->getMobie();
        if (!$app_id) {
            $this->exitMessage(false, '缺少参数');
        }
        if (empty($username)) {
            $this->exitMessage(false, '用户信息错误');
        }
        $bind_info = BindInfo::findFirst("username='{$username}'");
        if ($app_id == $bind_info->bind_app_id) {
            $this->exitMessage(false, '请勿重复绑定');
        }
        $bind_info->bind_app_id = $app_id;
        $res = $bind_info->save();
        if (!$res) {
            $this->exitMessage(false, '绑定失败');
        }
        $this->exitMessage(true, '绑定成功');
    }
}